package com.example.hasee.login;

import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.TransitionManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.base.BaseActivity;
import com.example.hasee.home.HomeActivity;
import com.example.hasee.libraryreservation.R;

public class LoginActivity extends BaseActivity implements LoginContract.View{
    private LoginContract.Presenter mPresenter;
    private ImageView imgLoginIcon;
    private TextView tvLoginTitle,tvSignUp;
    private EditText edtUsername,edtPassword,edtComfirm;
    private Button btnOk;
    private ConstraintLayout constraintLayout;
    private ConstraintSet loginSet,signUpSet;
    private boolean login=true;
    @Override
    protected int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {
        //初始化所有属性
        mPresenter=new LoginPresenter(this);
        imgLoginIcon=findViewById(R.id.imgLoginIcon);
        tvLoginTitle=findViewById(R.id.tvLoginTitle);
        tvSignUp=findViewById(R.id.tvSignUp);
        edtUsername=findViewById(R.id.edtUsername);
        edtPassword=findViewById(R.id.edtPassword);
        edtComfirm=findViewById(R.id.edtComfirm);
        btnOk=findViewById(R.id.btnOk);
        constraintLayout=findViewById(R.id.constraintLayout);
        signUpSet=new ConstraintSet();
        loginSet=new ConstraintSet();
        loginSet.clone(constraintLayout);//?
        signUpSet.clone(getApplicationContext(),R.layout.activity_sign_up);//?
    }

    @Override
    protected void setListener() {
        //设置监听器
        tvLoginTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!login){
                    TransitionManager.beginDelayedTransition(constraintLayout);
                    tvLoginTitle.setTextSize(30);
                    tvSignUp.setTextSize(15);
                    btnOk.setText("登陆");
                    imgLoginIcon.setImageResource(R.drawable.login);
                    loginSet.applyTo(constraintLayout);
                    login=!login;
                }
            }
        });
        tvSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(login){
                    TransitionManager.beginDelayedTransition(constraintLayout);
                    tvLoginTitle.setTextSize(15);
                    tvSignUp.setTextSize(30);
                    btnOk.setText("注册");
                    imgLoginIcon.setImageResource(R.drawable.signup);
                    signUpSet.applyTo(constraintLayout);
                    login=!login;
                }

            }
        });
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=edtUsername.getText().toString().trim();
                String pw=edtPassword.getText().toString().trim();
                String pc=edtComfirm.getText().toString().trim();
                if(login){
                    mPresenter.login(name,pw);
                }else {
                    if(pw.equals(pc)){
                        mPresenter.signUp(name,pw);
                    }else{
                        showError("两次密码不一致");
                    }
                }
            }
        });
    }

    @Override
    public void toHomeView() {
        //跳转到主界面
        toNewView(HomeActivity.class);
    }

    @Override
    public void showError(String message) {
        //显示错误信息
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoadingView() {
        //显示加载界面
    }

    @Override
    public void hideLoadingView() {
        //隐藏加载界面
    }
}
